package com.thuanpq.activity;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.thuanpq.R;
import com.thuanpq.database.bus.BlacklistBus;
import com.thuanpq.database.bus.WhitelistBus;
import com.thuanpq.entity.BlacklistItemEntity;
import com.thuanpq.entity.WhitelistItemEntity;
import com.thuanpq.view.HeaderController;

public class BlackListActivity extends Activity {
	private WhitelistBus mWhitelistBus;
	private WhitelistItemEntity[] mWhitelistItemEntities;
	private WhiteListItemLoader mWhiteListItemLoader;

	private BlacklistBus mBlacklistBus;
	private BlacklistItemEntity[] mBlacklistItemEntities;
	private BlackListItemLoader mBlackListItemLoader;

	private static final int WHITE_LIST = 0;
	private static final int BLACK_LIST = 1;
	private static int footerSelect = WHITE_LIST;
	private TextView tvWhiteList, tvBlackList;
	private LinearLayout lnWhiteListSelector, lnBlackListSelector;
	private ListView lvDisplay;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_display);
		footerSelect = WHITE_LIST;
		initChild();
		initView();
		initHeader();
	}

	@Override
	protected void onResume() {
		super.onResume();
		setListView();
	}

	private void initView() {
		lnWhiteListSelector = (LinearLayout) findViewById(R.id.footer_left_selector);
		lnBlackListSelector = (LinearLayout) findViewById(R.id.footer_right_selector);
		tvWhiteList = (TextView) findViewById(R.id.footer_left_tv);
		tvBlackList = (TextView) findViewById(R.id.footer_right_tv);
		lvDisplay = (ListView) findViewById(R.id.list_display_lv);
		setFooterListener();
	}

	private void initChild() {
		mWhitelistBus = new WhitelistBus(this);
		mBlacklistBus = new BlacklistBus(this);
	}

	private void initHeader() {
		HeaderController mController = new HeaderController(this);
		mController.getImbLeft().setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});

		mController.getImbRight().setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Dialog dialog = new Dialog(BlackListActivity.this);
				dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
				dialog.setContentView(R.layout.list_display_add_item);
				dialog.setCancelable(true);
				dialog.show();
			}
		});
	}

	private void setFooterListener() {
		lnWhiteListSelector.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (footerSelect != WHITE_LIST) {
					tvBlackList.setBackgroundDrawable(null);
					tvWhiteList
							.setBackgroundResource(R.drawable.transparent_stroke_image);
					footerSelect = WHITE_LIST;
					setListView();
				}
			}
		});

		lnBlackListSelector.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (footerSelect != BLACK_LIST) {
					tvWhiteList.setBackgroundDrawable(null);
					tvBlackList
							.setBackgroundResource(R.drawable.transparent_stroke_image);
					footerSelect = BLACK_LIST;
					setListView();
				}
			}
		});
	}

	private void setListView() {
		if (footerSelect == WHITE_LIST) {
			mWhitelistItemEntities = mWhitelistBus.getAllWhitelistItem();

			if (mWhitelistItemEntities != null
					&& mWhitelistItemEntities.length > 0) {
				mWhiteListItemLoader = new WhiteListItemLoader(
						mWhitelistItemEntities, this);
				lvDisplay.setAdapter(mWhiteListItemLoader);
			}
		} else if (footerSelect == BLACK_LIST) {
			mBlacklistItemEntities = mBlacklistBus.getAllBlacklistItem();

			if (mBlacklistItemEntities != null
					&& mBlacklistItemEntities.length > 0) {
				mBlackListItemLoader = new BlackListItemLoader(
						mBlacklistItemEntities, this);
				lvDisplay.setAdapter(mBlackListItemLoader);
			}
		}
	}

	private void saveWhiteListChanges() {
		mWhitelistBus.updateAllEntities(mWhitelistItemEntities);
	}

	private void saveBlackListChanges() {
		mBlacklistBus.updateAllEntities(mBlacklistItemEntities);
	}

	private void saveChanges() {
		if (footerSelect == WHITE_LIST)
			saveWhiteListChanges();
		else if (footerSelect == BLACK_LIST)
			saveBlackListChanges();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.blacklist_option_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.option_add:
			saveChanges();
			break;
		}
		return true;
	}
}